// pages/productSearchMob/productSearchMob.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword:'',
    arr1:[],
    // ordercount1:'yes',
    type:"",
    num1:0,
    num2:0,
    num3:0,
    num4:0,
    ordercount1:'yes',
    showWord:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //接受搜索参数
    var key = options.key;
    this.setData({
      keyword:key
    });
    console.log(this.data.keyword)
    this.searchCom();
  },
  // 双向绑定搜索框的值
  bindKeyInput:function(e){
    this.setData({
      keyword:e.detail.value
    })
  },
  // 搜索商品事件
  searchCom:function(){
    // console.log(this.data.keyword)
    let that = this;
    wx.request({
      url: 'http://47.104.142.216/TP/public/index.php/index/Productsearch/selectWxComInfo',//给函数传递服务器地址参数  
      data: {
        keyword:that.data.keyword,
        order:that.data.type,
        ordercounts:that.data.ordercount1
      },//给服务器传递数据，本次请求不需要数据，可以不填
      header: {
        'content-type': 'application/json' // 默认值，返回的数据设置为json数组格式
      },
      success: function (res) {
        that.setData({
          arr1:res.data
        })
        if(that.data.arr1.length == 0){
          that.setData({
            showWord:true
          })
        }else{
          that.setData({
            showWord:false
          })
        }
        console.log(that.data.showWord)
        console.log(that.data.arr1)
      },
    });
  },
  // 分类点击事件
  sortClick:function(e){ 
    this.setData({
      type:e.currentTarget.dataset['typeid'],
      ordercounts:e.currentTarget.dataset['istypeid']
    }) 
    console.log(this.data.type);
    console.log(this.data.ordercounts);
    if(this.data.ordercounts == 'issales'){
      this.data.num1++ 
      console.log(this.data.num1)
      if(this.data.num1 % 2 != 0){
        this.setData({
          num2:0,
          num3:0,
          num4:0,
          ordercount1:"no"
        })
        console.log(this.data.ordercount1)
      }else{
        this.setData({
          ordercount1:"yes"
        })
        console.log(this.data.ordercount1)
      }
    }
    if(this.data.ordercounts == 'istimes'){
      this.data.num2++;
      if(this.data.num2 % 2 != 0){
        this.setData({
          num1:0,
          num3:0,
          num4:0,
          ordercount1:"no"
        })
        
      }else{
        this.setData({
          ordercount1:"yes"
        })
      } 
    }
    if(this.data.ordercounts == 'isstock'){
      this.data.num3++;
      if(this.data.num3 % 2 != 0){
        this.setData({
          num1:0,
          num2:0,
          num4:0,
          ordercount1:"no"
        })
        
      }else{
        this.setData({
          ordercount1:"yes"
        })
      }
    }
    if(this.data.ordercounts == 'isprice'){
      this.data.num4++;
      if(this.data.num4 % 2 != 0){
        this.setData({
          num1:0,
          num2:0,
          num3:0,
          ordercount1:"no"
        })
        
      }else{
        this.setData({
          ordercount1:"yes"
        })
      }
    }
    this.searchCom();
  },
  // 搜索按钮事件
  searchClick:function(){
    this.searchCom();
  },
  // 跳转商品详情页
  goToProduct:function(e){
    console.log(e.currentTarget.dataset.proid);
    let id=e.currentTarget.dataset.proid;
    console.log(id)
    wx.navigateTo({
        url: '../goods/goods?cid='+id
    })
  }
})